#include <lpc23xx.h>
#include "LCD.h"

void T3isr (void) __irq; 
void T2isr (void) __irq; 
	
int main (void)
{
    
unsigned int delay; //
	/* Reset all GPIO pins to default: primary function */
    PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    PINSEL2 = 0x00000000;
    PINSEL3 = 0x00000000;
    PINSEL4 = 0x00000000;
    PINSEL5 = 0x00000000;
    PINSEL6 = 0x00000000;
    PINSEL7 = 0x00000000;
    PINSEL8 = 0x00000000;
    PINSEL9 = 0x00000000;
    PINSEL10 = 0x00000000;
    
    IODIR0 = 0x00000000;
    IODIR1 = 0x00000000;
	  IOSET0 = 0x00000000;
    IOSET1 = 0x00000000;
    
    FIO0DIR = 0x00000000;
    FIO1DIR = 0x00000000;
    FIO2DIR = 0x000000FF;
    FIO3DIR = 0x00000000;
    FIO4DIR = 0x00000000;
    
    FIO0SET = 0x00000000;
    FIO1SET = 0x00000000;
    FIO2SET = 0x00000000;
    FIO3SET = 0x00000000;
    FIO4SET = 0x00000000;					
   lcd_init();
   lcd_clear();
   lcd_print("Timer Capture");
   
PINSEL0      |= (3<<12);        //Enable Timer 2 match 1 pin
PCONP       |= (0x1<<22);       // Power up timer 2
T2PR 		     = 0x00000000;			//Load prescaler
T2TCR 		   = 0x00000002;			//Reset counter and prescaler
T2MCR 		   = 0x00000003;			//On match reset the counter and generate an interrupt
T2MR0		     = 60000000/4;			//Set the cycle time
T2MR1		     = 60000000/8;			// Set duty cycle to 50%
T2EMR  		   = 0x00000042;			//On match clear MAT1
T2TCR 		   = 0x00000001;			//enable timer
  
VICVectAddr26 = (unsigned)T2isr;//Set the timer ISR vector address
VICVectCntl26 = 0x00000000;			//Set channel
VICIntEnable |= 0x04000000;			//Enable the interrupt

PCONP     |= (1<<23);           //enable timer 3
   
PINSEL1 	|= (3<<14);				     //Enable pin 0.23 as capture channel0
T3PR 	  	= 0x00000000;					 //Load prescaler for 1 Msec tick
T3TCR 		= 0x00000002;					 //Reset counter and prescaler
T3CCR 		= 0x00000005;					 //Capture on rising edge of channel0,interrupt enabled
T3TCR 		= 0x00000001;					 //enable timer

VICVectAddr27 = (unsigned)T3isr;  //Set the timer ISR vector address
VICVectCntl27 = 0x00000024;			 //Set channel
VICIntEnable |= 0x08000000;			 //Enable the interrupt

while(1)
{
;
}
}


void T3isr (void)  __irq
{

static unsigned int RisingCount,FallingCount,Period;
//if rising store vale switch interrupts
if(T3CCR |= 0x00000001)
  {
    RisingCount = T3CR0;    //read the rising edge timer count
    T3CCR = 0x00000006;      //Enable the falling edge interrupt,disable rising edge interrupt
  }
if(T3CCR |= 0x00000002)
  {
    FallingCount = T3CR0;    //read the rising edge timer count
    T3CCR = 0x00000005;     //Enable the rising edge interrupt,disable falling edge interrupt
    Period = FallingCount - RisingCount;
        
  }

//if falling compute period switch interrupts 

T3IR 		    |= 0x00000001;					//Clear match 0 interrupt
VICVectAddr  = 0x00000000;					//Dummy write to signal end of interrupt
								   	
}
  
void T2isr (void)	__irq
{
T2EMR 		  |= 0x00000002;			//Set MAT1 high for begining of the cycle    
T2IR 		    |= 0x00000001;			    //Clear match 2 interrupt
VICVectAddr  = 0x00000000;			  //Dummy write to signal end of interrupt
}   